//
//  FishingEventViewController.m
//  WHTestDome
//
//  Created by Assistant on 9/22/25.
//

#import "FishingEventViewController.h"
#import "WHRegistrationListVC.h"

@interface FishingEventViewController ()

@property (nonatomic,assign)CGFloat viewHeight;

@end

@implementation FishingEventViewController

-(instancetype)initWithHeight:(CGFloat)viewHeight
{
    self = [super init];
    if (self)
    {   self.viewHeight = viewHeight;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"钓鱼比赛";
    
    [self setupMVC];
}

- (void)setupMVC {
    // 创建模型
    self.event = [FishingEvent mockEvent];
    self.remainingSeconds = 24 * 60 * 60 + 13 * 60 * 60 + 20 * 60 + 20; // 1天13小时20分20秒
    
    // 创建视图
    self.eventView = [[FishingEventView alloc] initWithFrame:CGRectMake(0, 0, ScreenW-40, self.viewHeight)];
    self.eventView.delegate = self;
    [self.view addSubview:self.eventView];
    
    // 更新视图数据
    [self.eventView updateWithEvent:self.event];
    
    // 开始倒计时
    [self startCountdownTimer];
}

#pragma mark - 业务逻辑方法

- (void)startCountdownTimer {
    [self stopCountdownTimer];
    self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                           target:self
                                                         selector:@selector(updateCountdown)
                                                         userInfo:nil
                                                          repeats:YES];
}

- (void)stopCountdownTimer {
    [self.countdownTimer invalidate];
    self.countdownTimer = nil;
}

- (void)updateCountdown {
    if (self.remainingSeconds <= 0) {
        [self stopCountdownTimer];
        return;
    }
    
    NSInteger days = self.remainingSeconds / (24 * 60 * 60);
    NSInteger hours = (self.remainingSeconds % (24 * 60 * 60)) / (60 * 60);
    NSInteger minutes = (self.remainingSeconds % (60 * 60)) / 60;
    NSInteger seconds = self.remainingSeconds % 60;
    
    NSString *countdownText = [NSString stringWithFormat:@"距离报名结束时间还剩: %02ld天 %02ld:%02ld:%02ld", 
                              (long)days, (long)hours, (long)minutes, (long)seconds];
    
    [self.eventView updateCountdownText:countdownText];
    self.remainingSeconds--;
}

- (void)registerForEvent {
    // 模拟报名逻辑
    NSLog(@"用户报名钓鱼活动: %@", self.event.eventName);
    
    // 更新已报名人数
    self.event.registeredCount++;
    [self.eventView updateRegistrationCount:self.event.registeredCount];
}

- (void)contactMerchant {
    // 联系商家逻辑
    NSLog(@"联系商家");
}

- (void)shareEvent {
    // 分享活动逻辑
    NSLog(@"分享活动: %@", self.event.eventName);
}

#pragma mark - FishingEventViewDelegate

- (void)fishingEventViewDidTapRegister {
    [self registerForEvent];
}

- (void)fishingEventViewDidTapContact {
    [self contactMerchant];
}

- (void)fishingEventViewDidTapShare {
    [self shareEvent];
}

- (void)fishingEventViewDidTapViewAll {
    NSLog(@"FishingEventViewController: 查看全部按钮被点击，准备跳转到报名列表");
    WHRegistrationListVC *vc = [[WHRegistrationListVC alloc] init];
//    vc.eventTitle = self.event.eventName;
//    vc.registeredCount = self.event.registeredCount;
//    vc.maxParticipants = self.event.maxParticipants;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)dealloc {
    [self stopCountdownTimer];
}

@end
